from machine import I2C
import machine
from ssd1306 import SSD1306_I2C
import math
import time



Num=[

    

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xE0,0x70,0x18,0x18,0x18,0x18,
    0x70,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xF8,0xFF,0x3F,0x03,0x00,
    0x00,0x00,0x00,0x00,0x00,0x01,0x7F,0xFF,0xF8,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,
    0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,
    0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
    0xFF,0xFF,0x00,0x00,0x00,0x00,0x03,0x7F,0xFF,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0xF8,0xFF,0x3F,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x07,0x1F,0x3C,
    0x30,0x30,0x30,0x30,0x38,0x1F,0x07,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,##0##0

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xE0,0xF8,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x01,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x30,
    0x38,0x3F,0x3F,0x3C,0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,##1##1

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xE0,0x30,0x18,0x18,0x18,0x18,0x18,
    0x38,0x70,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0xFF,0xFF,0xE0,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x8F,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
    0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xF0,0xFF,0x3F,0x07,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x60,0x38,0x0E,0x07,0x01,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0x38,0x1C,0x07,0x01,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x3C,0x3F,0x3C,0x3C,0x3C,0x3C,
    0x3C,0x3C,0x3C,0x3C,0x3C,0x3C,0x3C,0x3F,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,##2##2

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x70,0x10,0x18,0x18,0x18,0x18,0x38,
    0x70,0xF0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x7F,0x3C,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0xE0,0xB8,0x1F,0x0F,0x03,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x7E,0xFC,
    0xE0,0x00,0x00,0x00,0x00,0x00,0xE0,0xF8,0xF8,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0xC0,0xFF,0xFF,0x04,0x00,0x00,0x00,0x00,0x00,0x07,0x0F,0x1C,0x30,0x30,
    0x20,0x20,0x20,0x30,0x38,0x1C,0x0F,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,##3##3

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,
    0xF0,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0xC0,0x78,0x0E,0x03,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0xE0,0x38,0x0F,0x01,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0xE0,0xBC,0x87,0x81,0x80,0x80,0x80,0x80,0x80,0x80,0xFF,0xFF,0x80,0x80,
    0x80,0x80,0x80,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
    0xFF,0xFF,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x30,0x30,0x30,0x38,0x3F,0x3F,0x38,0x30,0x30,0x30,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,##4##4

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x78,0x78,0x78,0x78,0x78,0x78,0x78,
    0x78,0x78,0x78,0x78,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0xFF,0xC0,0x70,0x38,0x18,0x18,0x18,0x18,0x38,0xF8,0xF0,0xC0,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x03,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,
    0xFF,0x00,0x00,0x00,0x00,0x00,0x70,0xFC,0xFC,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0xC0,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0F,0x18,0x30,0x30,
    0x20,0x20,0x20,0x30,0x38,0x1C,0x0F,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,##5##5

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0x70,0x30,0x18,0x18,0x18,
    0x18,0xF8,0xF0,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xFE,0x1F,0x01,0x00,
    0x00,0x00,0x00,0x00,0x00,0x03,0x07,0x07,0x01,0x00,0x00,0x00,0x00,0x00,0xFE,0xFF,
    0xFF,0x80,0xE0,0x70,0x38,0x18,0x18,0x18,0x38,0x78,0xF0,0xE0,0x80,0x00,0x00,0x00,
    0x00,0x00,0xFF,0xFF,0xFF,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,
    0xFF,0xF8,0x00,0x00,0x00,0x00,0x0F,0xFF,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0xFF,0xFF,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0F,0x1E,0x38,
    0x30,0x20,0x20,0x20,0x30,0x18,0x0E,0x07,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,##6##6

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xF8,0xF8,0x78,0x78,0x78,0x78,0x78,0x78,
    0x78,0x78,0x78,0xF8,0xF8,0x18,0x00,0x00,0x00,0x00,0x00,0x3F,0x03,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0xE0,0x3C,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xF8,0x1F,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xFC,0xFF,0x03,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xFF,0xFF,0xE3,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,
    0x3F,0x3F,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,##7##7

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xF0,0x30,0x18,0x18,0x18,0x18,0x18,
    0x18,0x30,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x78,0xFF,0xFF,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x07,
    0x1F,0x3F,0x7C,0xF8,0xF0,0xE0,0xC0,0xC0,0xE0,0x30,0x1C,0x0F,0x03,0x00,0x00,0x00,
    0x00,0x00,0xC0,0xF0,0x7C,0x0E,0x03,0x01,0x01,0x01,0x03,0x07,0x0F,0x3F,0xFE,0xF8,
    0xE0,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0xFF,0xFF,0x3E,0x00,0x00,0x00,0x00,0x00,0x03,0x0F,0x1C,0x38,0x30,
    0x20,0x20,0x20,0x30,0x30,0x18,0x0E,0x07,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,##8##8

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0xF0,0x30,0x18,0x18,0x18,0x18,0x18,
    0x30,0xF0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0xFF,0x1F,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,
    0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0xFF,0xFF,0xFF,0x00,0x00,
    0x00,0x00,0x00,0x07,0x0F,0x1F,0x3C,0x38,0x30,0x30,0x38,0x18,0x0E,0x03,0xC0,0xFF,
    0xFF,0x1F,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0xE0,0xFF,0x7F,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x1F,0x3F,0x38,0x20,
    0x20,0x30,0x30,0x18,0x0E,0x07,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,##9##9
]

char=[
    0x00,0x00,0x00,0x70,0x88,0x88,0x70,0x00,0x80,0xC0,0x60,0x30,0x10,0x10,0x10,0x10,
    0x10,0x20,0x20,0xC0,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7E,
    0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x07,0x0C,0x08,0x18,0x10,0x10,0x10,
    0x08,0x08,0x04,0x02,0x00,0x00,0x00,0x00,##℃##0
    
    0x00,0x00,0x00,0x00,0xE0,0x10,0x10,0x10,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x80,0xC0,0x60,0x30,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x08,0x08,0x08,
    0x07,0x80,0x40,0x20,0x10,0x08,0x04,0xE3,0x21,0x10,0x20,0xC0,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x10,0x08,0x04,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x03,0x0F,
    0x10,0x10,0x10,0x0F,0x00,0x00,0x00,0x00,##％##1
      ]
icon=[
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,
0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0xFC,
0xFC,0xFC,0xFC,0x00,0xF0,0xF0,0xF0,0xF0,
0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0x00,
0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0x00,
0x0F,0x0F,0x0F,0x0F,0x00,0x00,0x00,0x0F,
0x0F,0x0F,0x0F,0x00,0x00,0x00,0x00,0x0F,
0x0F,0x0F,0x0F,0x00,
]


i2c = machine.I2C(-1, machine.Pin(4),machine.Pin(5), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)

def pixel(x,y,c):
    oled.pixel(x,y,c)
    

def invert(n):
    oled.invert(n)

def text(char,x,y):
    oled.text(char,x,y)

def fill(n):
    oled.fill(n)

def show():
    oled.show();

def poweroff():
    oled.poweroff()

def poweron():
    oled.poweron()

def DrawCircle(x,y,r,color,fill=0):
    if(fill==0):
        for i in range(x-r,x+r+1):
            oled.pixel(i,int(y-math.sqrt(r*r-(x-i)*(x-i))),color)
            oled.pixel(i,int(y+math.sqrt(r*r-(x-i)*(x-i))),color)
        for i in range(y-r,y+r+1):
            oled.pixel(int(x-math.sqrt(r*r-(y-i)*(y-i))),i,color)
            oled.pixel(int(x+math.sqrt(r*r-(y-i)*(y-i))),i,color)
    else:
        for i in range(x-r,x+r+1):
            a=int(math.sqrt(r*r-(x-i)*(x-i)))
            shuline(i,y-a,a*2,color)
        for i in range(y-r,y+r+1):
            a=int(math.sqrt(r*r-(y-i)*(y-i)))
            hline(x-a,i,a*2,color)             

def ByteOpera(num,dat):
    byte= [0x01,0x02,0x04,0x8,0x10,0x20,0x40,0x80]
    if dat&byte[num]:
        return 1
    else:
        return 0   


##def ShowChar16x16(x,y,n):
##    for i in range(2):
##        for a in range(16):
##            for b in range(8):
##                if(ByteOpera(b,chine[n*32+i*16+a])):
##                    oled.pixel(x+a,y+i*8+b,1)
##                else:
##                    oled.pixel(x+a,y+i*8+b,0)
                    
def ShowChar40x64(x,y,n):
    for i in range(8):
        for a in range (20):
            for b in range(8):
                temp=ByteOpera(b,Num[int(n)*160+i*20+a])
                if(temp):
                    oled.pixel(x+a,y+i*8+b,1)
                else:
                    oled.pixel(x+a,y+i*8+b,0)
                    
def ShowChar24x24(x,y,n):
    for i in range(3):
        for a in range (24):
            for b in range(8):
                temp=ByteOpera(b,char[int(n)*72+i*24+a])
                if(temp):
                    oled.pixel(x+a,y+i*8+b,1)
                else:
                    oled.pixel(x+a,y+i*8+b,0)

def ShowIcon(x,y):
    for i in range(3):
        for a in range (20):
            for b in range(8):
                temp=ByteOpera(b,icon[i*20+a])
                if(temp):
                    oled.pixel(x+a,y+i*8+b,1)
                else:
                    oled.pixel(x+a,y+i*8+b,0)



    
